<template>
<view class="container">
  <view class="chat_user_list">
    <view class="chat_user_item" v-for="(chatUserItem, index) in chatUserList" :key="index" :data-link="'chat_room.html?puid=' + chatUserItem.id + '&puname=' + chatUserItem.nickname" :data-id="chatUserItem.id" @click.stop="tolinkUrl">
      <view class="chat_user_item_left">
        <image class="user_icon" :src="chatUserItem.headimgurl"></image>
        <text class="unread_ount" :style="'background:' + setting.platformSetting.defaultColor" v-if="chatUserItem.unreadCount!=0">{{chatUserItem.unreadCount}}</text>
      </view>
      <view class="chat_user_item_right">
        <view class="chat_user_item_user">
          <text class="user_name">{{chatUserItem.nickname}}</text>
          <text class="user_news_msg">{{chatUserItem.lastChatContent}}</text>
        </view>
        <view class="chat_user_item_state">
          <text class="user_news_time">{{chatUserItem.lastChatTime}}</text>
          <!-- <text class=''></text> -->
        </view>
      </view>
    </view>
  </view>
</view>
</template>

<script>

export default {
  data() {
    return {
      chatUserList: [],

      /**
       * 生命周期函数--监听页面加载
       */
      options: {},
      listPage: {
        page: 1,
        pageSize: 0,
        totalSize: 0,
        curpage: 1
      },
      allNum: "",
      setting: "",
      loginUser: "",
      properties: ""
    };
  },

  components: {},
  props: {},
  onLoad: function (options) {
    console.log("===options===", options);
    this.options = options;
    this.getChatUserListFun(options);
    this.setData({
      setting: getApp().globalData.setting,
      loginUser: getApp().globalData.loginUser,
      properties: getApp().globalData.properties
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.favouriteList = [];
    this.listPage.page = 1;
    this.getChatUserListFun(this.options);
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;

    if (that.listPage.totalSize > that.listPage.curPage * that.listPage.pageSize) {
      that.listPage.page++;
      this.getChatUserListFun(this.options);
    }
  },
  methods: {
    tolinkUrl: function (data) {
      let that = this;
      console.log("data", data);
      let linkUrl = data.currentTarget ? data.currentTarget.dataset.link : data;
      let id = data.currentTarget ? data.currentTarget.dataset.id : data;
      let chatUserList = that.chatUserList;

      if (id) {
        for (let i = 0; i < chatUserList.length; i++) {
          if (chatUserList[i].id == id) {
            chatUserList[i].unreadCount = 0;
          }
        }
      }

      that.setData({
        chatUserList: chatUserList
      });
      console.log("==linkUrl===", linkUrl);
      getApp().globalData.linkEvent(linkUrl);
    },
    getChatUserListFun: function (options) {
      let that = this;
      let getParam = {};
      getParam.page = this.listPage.page;
      let customIndex = getApp().globalData.AddClientUrl("/wx_find_chat_users.html", getParam);
      getApp().globalData.showToastLoading('loading', true);
      wx.request({
        url: customIndex.url,
        header: getApp().globalData.header,
        success: function (res) {
          console.log(res.data);
          that.listPage.pageSize = res.data.relateObj.pageSize;
          that.listPage.curPage = res.data.relateObj.curPage;
          that.listPage.totalSize = res.data.relateObj.totalSize;
          that.setData({
            allNum: that.listPage.totalSize
          });
          let data = res.data.relateObj.result;

          for (let i = 0; i < data.length; i++) {
            data[i].lastChatTime = data[i].lastChatTime.replace(/-/ig, "/");
          }

          if (!data) {
            return;
          }

          if (data.length == 0 && that.chatUserList.length == 0 && that.listPage.curPage) {
            that.setData({
              chatUserList: null
            });
          } else {
            // 获取到的数据要添加进原先的数据
            let newList = that.chatUserList;

            if (that.listPage.curPage != 1) {
              newList = newList.concat(data);
            } else {
              newList = data;
            }

            console.log(newList);
            that.setData({
              chatUserList: newList
            });
          }

          wx.hideLoading();
        },
        fail: function (res) {
          wx.hideLoading();
          getApp().globalData.loadFail();
        }
      });
    },
    setData: function (obj) {
      let that = this;
      let keys = [];
      let val, data;
      Object.keys(obj).forEach(function (key) {
        keys = key.split('.');
        val = obj[key];
        data = that.$data;
        keys.forEach(function (key2, index) {
          if (index + 1 == keys.length) {
            that.$set(data, key2, val);
          } else {
            if (!data[key2]) {
              that.$set(data, key2, {});
            }
          }

          data = data[key2];
        });
      });
    }
  }
};
</script>
<style>
@import "./index.css";
</style>